package com.callbox.frontend;

import java.util.ArrayList;
import java.util.List;

import com.callbox.R;
import com.callbox.dao.ClsMessageDAO;
import com.callbox.entity.ClsMessages;
import com.callbox.entity.clsMessage;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;


import android.widget.AdapterView.OnItemClickListener;

public class MessageActivity extends Activity {

	static final String[] MOBILE_OS = 
            new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"};

	private static final String TAG = "NO";
	static String  pathResource  ="";
	
	List<ClsMessages> arrayMessages = new ArrayList();
	
	/*private final List arrayMessages = new ArrayList();*/
	
	final String [] itemsLongClick =new String []{"Escuchar","Enviar","Eliminar", "Llamar"};
	final String [] itemsSend =new String []{"Correo","MMS","Bluetooth"};
	final String [] itemsMenu =new String []{"Buscar"};
	
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_message);
	        //setListAdapter(new MessageArrayAdapter(this, MOBILE_OS));
	        
	       arrayMessages = ClsMessageDAO.Instance().ListByMobile(this, 1);
	      
	       /* clsMessage objMessage = new clsMessage();
	        objMessage.set_number("+220081");
	        objMessage.set_date("07/07/2012");
	        objMessage.set_time("14:22");
	        objMessage.set_duration("00:22");
	        arrayMessages.add(objMessage);
	        
	        objMessage = new clsMessage();
	        objMessage.set_number("Max Izquierdo");
	        objMessage.set_date("07/07/2012");
	        objMessage.set_time("10:02");
	        objMessage.set_duration("00:12");
	        arrayMessages.add(objMessage);
	        
	        objMessage = new clsMessage();
	        objMessage.set_number("Andrez Otiniano");
	        objMessage.set_date("06/07/2012");
	        objMessage.set_time("19:52");
	        objMessage.set_duration("01:22");
	        arrayMessages.add(objMessage);	       
	        objMessage = new clsMessage();
	        objMessage.set_number("+949851420");
	        objMessage.set_date("05/07/2012");
	        objMessage.set_time("11:37");
	        objMessage.set_duration("02:46");
	        arrayMessages.add(objMessage);	*/
	        
	        final Context currentContext = this;
	        
	        MessageArrayAdapter adaptador = new MessageArrayAdapter(this);			
			ListView lstActividades = (ListView) findViewById(R.id.lstMessage);
			
			lstActividades.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
				
			    public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
			    	
			    	ClsMessages objA = (ClsMessages) arrayMessages.get(pos);
			    	
			    	final String phone = objA.getNumberFrom();
			    	
			    	AlertDialog.Builder builder = new AlertDialog.Builder(currentContext);
			    	builder.setTitle("Acciones");
			    	
			    	builder.setItems(itemsLongClick, new OnClickListener() {
			    		public void onClick(DialogInterface dialog, int which) {
			    			
			    			switch (which) {
							case 0:
								
								break;
								
							case 1:
								
								AlertDialog.Builder builderSend = new AlertDialog.Builder(currentContext);
								builderSend.setTitle("Enviar");
						    	
								builderSend.setItems(itemsSend, new OnClickListener() {	
									public void onClick(DialogInterface dialog, int which) {
										
										
										
									}					    		
						    	});
								builderSend.show();	
								
								break;
								
							case 2:
								
								break;
								
                            case 3:
                            	
                            	Intent intent = new Intent(Intent.ACTION_CALL);
                            	intent.setData(Uri.parse("tel:" + phone));
                            	currentContext.startActivity(intent);

								
								break;

							default:
								break;
							}			    		
			    		}
			    		});

			    	builder.create().show();		    	
			    	
			    	return true;
			    }
			});
			
			OnItemClickListener listener = new OnItemClickListener() {
			      public void onItemClick(AdapterView<?> parent, View view, int position,
			          long id) {
			    	  
			    	  ClsMessages objMessage = (ClsMessages) parent.getItemAtPosition(position);
			    	  
			    	  pathResource = objMessage.getPathResource();
			    	  
			    	  /*setTitle(objMessage.getNumberFrom());
			    	  
			    	  Intent i = new Intent(getApplicationContext(),PlayCall.class);
					  startActivity(i);*/
			      
			      }
			    };
			    
			    lstActividades.setOnItemClickListener(listener);
			    
			lstActividades.setAdapter(adaptador);
	    }
	 
	 protected boolean onLongListItemClick(View v, int pos, long id) {
		    Log.i(TAG, "onLongListItemClick id=" + id);
		    return true;
		}
	 
	 
	 @Override
	 public boolean onKeyDown(int keycode, KeyEvent event ) {
		 if(keycode == KeyEvent.KEYCODE_MENU){
			 AlertDialog.Builder dialogBuilder
			 = new AlertDialog.Builder(this).setTitle("Menu").setItems(itemsMenu, new OnClickListener() {
		    		public void onClick(DialogInterface dialog, int which) {
		    			switch (which) {
						case 0:
							
							
							break;
						case 2:
							
							break;

						default:
							break;
						}
		    			
		    			
		    		}});
			 
			 dialogBuilder.create().show();
	  }
	  return super.onKeyDown(keycode,event); 
	 }
	 
	 
	  @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        getMenuInflater().inflate(R.menu.activity_records, menu);
	        return true;
	    }
	 
	  class MessageArrayAdapter extends ArrayAdapter{
						
			Activity context;
			MessageArrayAdapter(Activity context)
			{
				super(context, R.layout.list_message, arrayMessages);
				this.context = context;
			}
			
		 
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				LayoutInflater inflater = context.getLayoutInflater();
				View item = inflater.inflate(R.layout.list_message, null);				
				
				ClsMessages objA = (ClsMessages) arrayMessages.get(position);
				
				TextView lblContact = (TextView)item.findViewById(R.id.lblContact);
				lblContact.setText(objA.getNumberFrom());
				
				TextView lblDetail = (TextView)item.findViewById(R.id.lblDetail);
				lblDetail.setText(objA.getReceiptDate() + " " + objA.getTime() + "         " + objA.getDuration());		
				
				return(item);
			}
		}
}
